package com.student.dao;

import com.student.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author lin945
 */
public class BaseDao {

    private QueryRunner queryRunner = new QueryRunner();

    /**
     * 基础更新update有参；
     *
     * @param sql sql
     * @param args 参数
     * @return int
     */

    public int update(String sql,Object... args) {
        Connection connection = JdbcUtils.getConnection();
        if (connection == null) {
            return -1;
        }
        try {
            return queryRunner.update(connection, String.valueOf(sql), args);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null, null, connection);
        }
        return -1;
    }

    /**
     * 无参数
     *
     * @param sql sql
     * @return  int
     */
    public int update(String sql) {
        Connection connection = JdbcUtils.getConnection();
        if (connection == null) {
            return -1;
        }
        try {
            return queryRunner.update(connection, String.valueOf(sql));
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(null, null, connection);
        }
        return -1;
    }

    /**javascript:;
     *
     * @param type bean
     * @param sql sql语句
     * @param args 参数
     * @param <T> bean.class 传什么类返回什么对象
     * @return bean
     */

    public <T> T querForOne(Class<T> type, String sql, Object... args) {
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.query(connection, sql, new BeanHandler<T>(type), args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,null,connection);
        }
        return null;
    }

    /**
     *
     * @param type 类型
     * @param sql sql
     * @param args 参数
     * @param <T> 泛型
     * @return list
     */
    public <T> List<T> querForList(Class<T> type, String sql, Object... args){
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.query(connection, String.valueOf(sql), new BeanListHandler<T>(type), args);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,null,connection);
        }
        return null;

    }

    public <T> List<T> querForList(Class<T> type, String sql){
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.query(connection, String.valueOf(sql), new BeanListHandler<T>(type));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,null,connection);
        }
        return null;

    }


     public  long userCount(String sql){
        Connection connection = JdbcUtils.getConnection();
        try {
            return queryRunner.query(connection, sql, new ScalarHandler<Long>());
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.close(null,null,connection);
        }
        return -1;
    };

}
